package cn.cyh.basic.uitls;

import cn.cyh.user.domain.LoginInfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;

public class LoginContext {

    public static LoginInfo currentLongInfo(HttpServletRequest request){
        // 获取token
        String token = request.getHeader("token");
        if (!StringUtils.isEmpty(token)){
            // 获取spring容器对象
            WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
            RedisTemplate redisTemlate = (RedisTemplate)webApplicationContext.getBean("redisTemplate");
            Object loginInfoTmp = redisTemlate.opsForValue().get(token);
            if (loginInfoTmp != null){
                return (LoginInfo)loginInfoTmp;
            }
        }
        return null;
    }
}
